{
    "cells": [
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "<i>Copyright (c) Recommenders contributors.</i>\n",
                "\n",
                "<i>Licensed under the MIT License.</i>"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "# Benchmark with Movielens dataset\n",
                "\n",
                "This illustrative comparison applies to collaborative filtering algorithms available in this repository such as Spark ALS, Surprise SVD, SAR and others using the Movielens dataset. These algorithms are usable in a variety of recommendation tasks, including product or news recommendations.\n",
                "\n",
                "The main purpose of this notebook is not to produce comprehensive benchmarking results on multiple datasets. Rather, it is intended to illustrate on how one could evaluate different recommender algorithms using tools in this repository.\n",
                "\n",
                "## Experimentation setup:\n",
                "\n",
                "* Objective\n",
                "  * To compare how each collaborative filtering algorithm perform in predicting ratings and recommending relevant items.\n",
                "\n",
                "* Environment\n",
                "  * The comparison is run on a [Azure Data Science Virtual Machine](https://azure.microsoft.com/en-us/services/virtual-machines/data-science-virtual-machines/). \n",
                "  * The virtual machine size is a [Standard_NC6s_v2](https://learn.microsoft.com/es-es/azure/virtual-machines/ncv2-series) with 6 CPUs, 112Gb of RAM, and 1 GPU NVIDIA Tesla P100 with 16Gb of memory.\n",
                "  * It should be noted that the single node DSVM is not supposed to run scalable benchmarking analysis. Either scaling up or out the computing instances is necessary to run the benchmarking in an run-time efficient way without any memory issue.\n",
                "  * **NOTE ABOUT THE DEPENDENCIES TO INSTALL**: This notebook uses CPU, GPU and PySpark algorithms, so make sure you install the `full environment` as detailed in the [SETUP.md](../../SETUP.md). \n",
                "  \n",
                "* Datasets\n",
                "  * [Movielens 100K](https://grouplens.org/datasets/movielens/100k/).\n",
                "  * [Movielens 1M](https://grouplens.org/datasets/movielens/1m/).\n",
                "\n",
                "* Data split\n",
                "  * The data is split into train and test sets.\n",
                "  * The split ratios are 75-25 for train and test datasets.\n",
                "  * The splitting is stratified based on items. \n",
                "\n",
                "* Model training\n",
                "  * A recommendation model is trained by using each of the collaborative filtering algorithms. \n",
                "  * Empirical parameter values reported [here](http://mymedialite.net/examples/datasets.html) are used in this notebook.  More exhaustive hyper parameter tuning would be required to further optimize results.\n",
                "\n",
                "* Evaluation metrics\n",
                "  * Ranking metrics:\n",
                "    * Precision@k.\n",
                "    * Recall@k.\n",
                "    * Normalized discounted cumulative gain@k (NDCG@k).\n",
                "    * Mean-average-precision (MAP). \n",
                "    * In the evaluation metrics above, k = 10. \n",
                "  * Rating metrics:\n",
                "    * Root mean squared error (RMSE).\n",
                "    * Mean average error (MAE).\n",
                "    * R squared.\n",
                "    * Explained variance.\n",
                "  * Run time performance\n",
                "    * Elapsed for training a model and using a model for predicting/recommending k items. \n",
                "    * The time may vary across different machines. "
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## Globals settings"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 2,
            "metadata": {},
            "outputs": [],
            "source": [
                "import warnings\n",
                "warnings.filterwarnings(\"ignore\")\n",
                "import logging\n",
                "logging.basicConfig(level=logging.ERROR) "
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 11,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "System version: 3.7.13 (default, Mar 29 2022, 02:18:16) \n",
                        "[GCC 7.5.0]\n",
                        "Number of cores: 6\n",
                        "NumPy version: 1.21.6\n",
                        "Pandas version: 1.3.5\n",
                        "Surprise version: 1.1.1\n",
                        "Cornac version: 1.14.2\n",
                        "PySpark version: 3.2.2\n",
                        "CUDA version: 10.2\n",
                        "CuDNN version: 7605\n",
                        "TensorFlow version: 2.7.4\n",
                        "PyTorch version: 1.12.1+cu102\n",
                        "Fast AI version: 1.0.61\n",
                        "The autoreload extension is already loaded. To reload it, use:\n",
                        "  %reload_ext autoreload\n"
                    ]
                }
            ],
            "source": [
                "import os\n",
                "import sys\n",
                "import numpy as np\n",
                "import pandas as pd\n",
                "import surprise\n",
                "import cornac\n",
                "\n",
                "try:\n",
                "    import pyspark\n",
                "except ImportError:\n",
                "    pass  # skip this import if we are not in a Spark environment\n",
                "\n",
                "try:\n",
                "    import tensorflow as tf # NOTE: TF needs to be imported before PyTorch, otherwise we get an error\n",
                "    tf.get_logger().setLevel('ERROR') # only show error messages\n",
                "    import torch\n",
                "    import fastai\n",
                "except ImportError:\n",
                "    pass  # skip this import if we are not in a GPU environment\n",
                "\n",
                "current_path = os.path.join(os.getcwd(), \"examples\", \"06_benchmarks\") # To execute the notebook programmatically from root folder\n",
                "sys.path.append(current_path)\n",
                "from benchmark_utils import * \n",
                "\n",
                "from recommenders.datasets import movielens\n",
                "from recommenders.utils.general_utils import get_number_processors\n",
                "from recommenders.datasets.python_splitters import python_stratified_split\n",
                "try:\n",
                "    from recommenders.utils.spark_utils import start_or_get_spark\n",
                "except ImportError:\n",
                "    pass  # skip this import if we are not in a Spark environment\n",
                "try:\n",
                "    from recommenders.utils.gpu_utils import get_cuda_version, get_cudnn_version\n",
                "    from recommenders.models.fastai.fastai_utils import hide_fastai_progress_bar\n",
                "    hide_fastai_progress_bar()\n",
                "except ImportError:\n",
                "    pass  # skip this import if we are not in a GPU environment\n",
                "from recommenders.utils.notebook_utils import store_metadata\n",
                "\n",
                "\n",
                "print(f\"System version: {sys.version}\")\n",
                "print(f\"Number of cores: {get_number_processors()}\")\n",
                "print(f\"NumPy version: {np.__version__}\")\n",
                "print(f\"Pandas version: {pd.__version__}\")\n",
                "print(f\"Surprise version: {surprise.__version__}\")\n",
                "print(f\"Cornac version: {cornac.__version__}\")\n",
                "try:\n",
                "    print(f\"PySpark version: {pyspark.__version__}\")\n",
                "except NameError:\n",
                "    pass  # skip this import if we are not in a Spark environment\n",
                "try:\n",
                "    print(f\"CUDA version: {get_cuda_version()}\")\n",
                "    print(f\"CuDNN version: {get_cudnn_version()}\")\n",
                "    print(f\"TensorFlow version: {tf.__version__}\")\n",
                "    print(f\"PyTorch version: {torch.__version__}\")\n",
                "    print(f\"Fast AI version: {fastai.__version__}\")\n",
                "except NameError:\n",
                "    pass  # skip this import if we are not in a GPU environment\n",
                "\n",
                "%load_ext autoreload\n",
                "%autoreload 2"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 6,
            "metadata": {},
            "outputs": [],
            "source": [
                "try:\n",
                "    spark = start_or_get_spark(\"PySpark\", memory=\"32g\")\n",
                "    spark.conf.set(\"spark.sql.analyzer.failAmbiguousSelfJoin\", \"false\")\n",
                "except NameError:\n",
                "    pass  # skip this import if we are not in a Spark environment"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 9,
            "metadata": {},
            "outputs": [],
            "source": [
                "# fix random seeds to make sure out runs are reproducible\n",
                "np.random.seed(SEED)\n",
                "try:\n",
                "    tf.random.set_seed(SEED)\n",
                "    torch.manual_seed(SEED)\n",
                "    torch.cuda.manual_seed_all(SEED)\n",
                "except NameError:\n",
                "    pass  # skip this import if we are not in a GPU environment"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## Parameters"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {
                "tags": [
                    "parameters"
                ]
            },
            "outputs": [],
            "source": [
                "data_sizes = [\"100k\", \"1m\"] # Movielens data size: 100k, 1m, 10m, or 20m\n",
                "algorithms = [\"als\", \"svd\", \"sar\", \"ncf\", \"fastai\", \"bpr\", \"bivae\", \"lightgcn\"]"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 6,
            "metadata": {},
            "outputs": [],
            "source": [
                "environments = {\n",
                "    \"als\": \"pyspark\",\n",
                "    \"sar\": \"python_cpu\",\n",
                "    \"svd\": \"python_cpu\",\n",
                "    \"fastai\": \"python_gpu\",\n",
                "    \"ncf\": \"python_gpu\",\n",
                "    \"bpr\": \"python_cpu\",\n",
                "    \"bivae\": \"python_gpu\",\n",
                "    \"lightgcn\": \"python_gpu\",\n",
                "}\n",
                "\n",
                "metrics = {\n",
                "    \"als\": [\"rating\", \"ranking\"],\n",
                "    \"sar\": [\"ranking\"],\n",
                "    \"svd\": [\"rating\", \"ranking\"],\n",
                "    \"fastai\": [\"rating\", \"ranking\"],\n",
                "    \"ncf\": [\"ranking\"],\n",
                "    \"bpr\": [\"ranking\"],\n",
                "    \"bivae\": [\"ranking\"],\n",
                "    \"lightgcn\": [\"ranking\"]\n",
                "}"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "Algorithm parameters"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 7,
            "metadata": {},
            "outputs": [],
            "source": [
                "als_params = {\n",
                "    \"rank\": 10,\n",
                "    \"maxIter\": 20,\n",
                "    \"implicitPrefs\": False,\n",
                "    \"alpha\": 0.1,\n",
                "    \"regParam\": 0.05,\n",
                "    \"coldStartStrategy\": \"drop\",\n",
                "    \"nonnegative\": False,\n",
                "    \"userCol\": DEFAULT_USER_COL,\n",
                "    \"itemCol\": DEFAULT_ITEM_COL,\n",
                "    \"ratingCol\": DEFAULT_RATING_COL,\n",
                "}\n",
                "\n",
                "sar_params = {\n",
                "    \"similarity_type\": \"jaccard\",\n",
                "    \"time_decay_coefficient\": 30,\n",
                "    \"time_now\": None,\n",
                "    \"timedecay_formula\": True,\n",
                "    \"col_user\": DEFAULT_USER_COL,\n",
                "    \"col_item\": DEFAULT_ITEM_COL,\n",
                "    \"col_rating\": DEFAULT_RATING_COL,\n",
                "    \"col_timestamp\": DEFAULT_TIMESTAMP_COL,\n",
                "}\n",
                "\n",
                "svd_params = {\n",
                "    \"n_factors\": 150,\n",
                "    \"n_epochs\": 15,\n",
                "    \"lr_all\": 0.005,\n",
                "    \"reg_all\": 0.02,\n",
                "    \"random_state\": SEED,\n",
                "    \"verbose\": False\n",
                "}\n",
                "\n",
                "fastai_params = {\n",
                "    \"n_factors\": 40, \n",
                "    \"y_range\": [0,5.5], \n",
                "    \"wd\": 1e-1,\n",
                "    \"lr_max\": 5e-3,\n",
                "    \"epochs\": 15\n",
                "}\n",
                "\n",
                "ncf_params = {\n",
                "    \"model_type\": \"NeuMF\",\n",
                "    \"n_factors\": 4,\n",
                "    \"layer_sizes\": [16, 8, 4],\n",
                "    \"n_epochs\": 15,\n",
                "    \"batch_size\": 1024,\n",
                "    \"learning_rate\": 1e-3,\n",
                "    \"verbose\": 10\n",
                "}\n",
                "\n",
                "bpr_params = {\n",
                "    \"k\": 200,\n",
                "    \"max_iter\": 200,\n",
                "    \"learning_rate\": 0.01,\n",
                "    \"lambda_reg\": 1e-3,\n",
                "    \"seed\": SEED,\n",
                "    \"verbose\": False\n",
                "}\n",
                "\n",
                "bivae_params = {\n",
                "    \"k\": 100,\n",
                "    \"encoder_structure\": [200],\n",
                "    \"act_fn\": \"tanh\",\n",
                "    \"likelihood\": \"pois\",\n",
                "    \"n_epochs\": 500,\n",
                "    \"batch_size\": 1024,\n",
                "    \"learning_rate\": 0.001,\n",
                "    \"seed\": SEED,\n",
                "    \"use_gpu\": True,\n",
                "    \"verbose\": False\n",
                "}\n",
                "\n",
                "lightgcn_param = {\n",
                "    \"model_type\": \"lightgcn\",\n",
                "    \"n_layers\": 3,\n",
                "    \"batch_size\": 1024,\n",
                "    \"embed_size\": 64,\n",
                "    \"decay\": 0.0001,\n",
                "    \"epochs\": 20,\n",
                "    \"learning_rate\": 0.005,\n",
                "    \"eval_epoch\": 5,\n",
                "    \"top_k\": DEFAULT_K,\n",
                "    \"metrics\": [\"recall\", \"ndcg\", \"precision\", \"map\"],\n",
                "    \"save_model\":False,\n",
                "    \"MODEL_DIR\":\".\",\n",
                "}\n",
                "\n",
                "params = {\n",
                "    \"als\": als_params,\n",
                "    \"sar\": sar_params,\n",
                "    \"svd\": svd_params,\n",
                "    \"fastai\": fastai_params,\n",
                "    \"ncf\": ncf_params,\n",
                "    \"bpr\": bpr_params,\n",
                "    \"bivae\": bivae_params,\n",
                "    \"lightgcn\": lightgcn_param,\n",
                "}"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 8,
            "metadata": {},
            "outputs": [],
            "source": [
                "prepare_training_data = {\n",
                "    \"als\": prepare_training_als,\n",
                "    \"sar\": prepare_training_sar,\n",
                "    \"svd\": prepare_training_svd,\n",
                "    \"fastai\": prepare_training_fastai,\n",
                "    \"ncf\": prepare_training_ncf,\n",
                "    \"bpr\": prepare_training_cornac,\n",
                "    \"bivae\": prepare_training_cornac,\n",
                "    \"lightgcn\": prepare_training_lightgcn,\n",
                "}"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 9,
            "metadata": {},
            "outputs": [],
            "source": [
                "prepare_metrics_data = {\n",
                "    \"als\": lambda train, test: prepare_metrics_als(train, test),\n",
                "    \"fastai\": lambda train, test: prepare_metrics_fastai(train, test),    \n",
                "}"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 10,
            "metadata": {},
            "outputs": [],
            "source": [
                "trainer = {\n",
                "    \"als\": lambda params, data: train_als(params, data),\n",
                "    \"svd\": lambda params, data: train_svd(params, data),\n",
                "    \"sar\": lambda params, data: train_sar(params, data), \n",
                "    \"fastai\": lambda params, data: train_fastai(params, data),\n",
                "    \"ncf\": lambda params, data: train_ncf(params, data),\n",
                "    \"bpr\": lambda params, data: train_bpr(params, data),\n",
                "    \"bivae\": lambda params, data: train_bivae(params, data),\n",
                "    \"lightgcn\": lambda params, data: train_lightgcn(params, data),\n",
                "}"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 11,
            "metadata": {},
            "outputs": [],
            "source": [
                "rating_predictor = {\n",
                "    \"als\": lambda model, test: predict_als(model, test),\n",
                "    \"svd\": lambda model, test: predict_svd(model, test),\n",
                "    \"fastai\": lambda model, test: predict_fastai(model, test),\n",
                "}"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 12,
            "metadata": {},
            "outputs": [],
            "source": [
                "ranking_predictor = {\n",
                "    \"als\": lambda model, test, train: recommend_k_als(model, test, train),\n",
                "    \"sar\": lambda model, test, train: recommend_k_sar(model, test, train),\n",
                "    \"svd\": lambda model, test, train: recommend_k_svd(model, test, train),\n",
                "    \"fastai\": lambda model, test, train: recommend_k_fastai(model, test, train),\n",
                "    \"ncf\": lambda model, test, train: recommend_k_ncf(model, test, train),\n",
                "    \"bpr\": lambda model, test, train: recommend_k_cornac(model, test, train),\n",
                "    \"bivae\": lambda model, test, train: recommend_k_cornac(model, test, train),\n",
                "    \"lightgcn\": lambda model, test, train: recommend_k_lightgcn(model, test, train),\n",
                "}"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 13,
            "metadata": {},
            "outputs": [],
            "source": [
                "rating_evaluator = {\n",
                "    \"als\": lambda test, predictions: rating_metrics_pyspark(test, predictions),\n",
                "    \"svd\": lambda test, predictions: rating_metrics_python(test, predictions),\n",
                "    \"fastai\": lambda test, predictions: rating_metrics_python(test, predictions)\n",
                "}\n",
                "    \n",
                "    \n",
                "ranking_evaluator = {\n",
                "    \"als\": lambda test, predictions, k: ranking_metrics_pyspark(test, predictions, k),\n",
                "    \"sar\": lambda test, predictions, k: ranking_metrics_python(test, predictions, k),\n",
                "    \"svd\": lambda test, predictions, k: ranking_metrics_python(test, predictions, k),\n",
                "    \"fastai\": lambda test, predictions, k: ranking_metrics_python(test, predictions, k),\n",
                "    \"ncf\": lambda test, predictions, k: ranking_metrics_python(test, predictions, k),\n",
                "    \"bpr\": lambda test, predictions, k: ranking_metrics_python(test, predictions, k),\n",
                "    \"bivae\": lambda test, predictions, k: ranking_metrics_python(test, predictions, k),\n",
                "    \"lightgcn\": lambda test, predictions, k: ranking_metrics_python(test, predictions, k),\n",
                "}"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 14,
            "metadata": {},
            "outputs": [],
            "source": [
                "def generate_summary(data, algo, k, train_time, time_rating, rating_metrics, time_ranking, ranking_metrics):\n",
                "    summary = {\"Data\": data, \"Algo\": algo, \"K\": k, \"Train time (s)\": train_time, \"Predicting time (s)\": time_rating, \"Recommending time (s)\": time_ranking}\n",
                "    if rating_metrics is None:\n",
                "        rating_metrics = {\n",
                "            \"RMSE\": np.nan,\n",
                "            \"MAE\": np.nan,\n",
                "            \"R2\": np.nan,\n",
                "            \"Explained Variance\": np.nan,\n",
                "        }\n",
                "    if ranking_metrics is None:\n",
                "        ranking_metrics = {\n",
                "            \"MAP\": np.nan,\n",
                "            \"nDCG@k\": np.nan,\n",
                "            \"Precision@k\": np.nan,\n",
                "            \"Recall@k\": np.nan,\n",
                "        }\n",
                "    summary.update(rating_metrics)\n",
                "    summary.update(ranking_metrics)\n",
                "    return summary"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## Benchmark loop"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 16,
            "metadata": {
                "scrolled": true
            },
            "outputs": [
                {
                    "name": "stderr",
                    "output_type": "stream",
                    "text": [
                        "100%|██████████| 4.81k/4.81k [00:00<00:00, 12.5kKB/s]\n"
                    ]
                },
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "Size of Movielens 100k: (100000, 4)\n",
                        "\n",
                        "Computing als algorithm on Movielens 100k\n"
                    ]
                },
                {
                    "name": "stderr",
                    "output_type": "stream",
                    "text": [
                        "                                                                                \r"
                    ]
                },
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "Training time: 6.8526s\n",
                        "Rating prediction time: 0.0587s\n"
                    ]
                },
                {
                    "name": "stderr",
                    "output_type": "stream",
                    "text": [
                        "22/10/19 08:58:41 WARN Column: Constructing trivially true equals predicate, 'userID#225 = userID#225'. Perhaps you need to use aliases.\n"
                    ]
                },
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "Ranking prediction time: 0.0782s\n"
                    ]
                },
                {
                    "name": "stderr",
                    "output_type": "stream",
                    "text": [
                        "                                                                                \r"
                    ]
                },
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "\n",
                        "Computing svd algorithm on Movielens 100k\n",
                        "Training time: 4.0902s\n",
                        "Rating prediction time: 0.2698s\n",
                        "Ranking prediction time: 13.8704s\n",
                        "\n",
                        "Computing sar algorithm on Movielens 100k\n",
                        "Training time: 0.3344s\n",
                        "Ranking prediction time: 0.0836s\n",
                        "\n",
                        "Computing ncf algorithm on Movielens 100k\n",
                        "Training time: 66.8339s\n",
                        "Ranking prediction time: 3.5393s\n",
                        "\n",
                        "Computing fastai algorithm on Movielens 100k\n",
                        "Training time: 69.7469s\n",
                        "Rating prediction time: 0.0338s\n",
                        "Ranking prediction time: 2.7415s\n",
                        "\n",
                        "Computing bpr algorithm on Movielens 100k\n",
                        "Training time: 5.8205s\n",
                        "Ranking prediction time: 1.9365s\n",
                        "\n",
                        "Computing bivae algorithm on Movielens 100k\n",
                        "Training time: 11.4762s\n",
                        "Ranking prediction time: 1.4382s\n",
                        "\n",
                        "Computing lightgcn algorithm on Movielens 100k\n",
                        "Already create adjacency matrix.\n",
                        "Already normalize adjacency matrix.\n",
                        "Using xavier initialization.\n",
                        "Epoch 1 (train)0.9s: train loss = 0.47340 = (mf)0.47316 + (embed)0.00024\n",
                        "Epoch 2 (train)0.8s: train loss = 0.28803 = (mf)0.28739 + (embed)0.00064\n",
                        "Epoch 3 (train)0.8s: train loss = 0.25425 = (mf)0.25343 + (embed)0.00082\n",
                        "Epoch 4 (train)0.8s: train loss = 0.23797 = (mf)0.23699 + (embed)0.00098\n",
                        "Epoch 5 (train)0.8s + (eval)0.2s: train loss = 0.22717 = (mf)0.22605 + (embed)0.00111, recall = 0.16053, ndcg = 0.34968, precision = 0.30276, map = 0.09269\n",
                        "Epoch 6 (train)0.8s: train loss = 0.22202 = (mf)0.22081 + (embed)0.00121\n",
                        "Epoch 7 (train)0.8s: train loss = 0.21388 = (mf)0.21256 + (embed)0.00132\n",
                        "Epoch 8 (train)0.8s: train loss = 0.20578 = (mf)0.20434 + (embed)0.00144\n",
                        "Epoch 9 (train)0.8s: train loss = 0.19345 = (mf)0.19186 + (embed)0.00158\n",
                        "Epoch 10 (train)0.8s + (eval)0.2s: train loss = 0.18439 = (mf)0.18265 + (embed)0.00175, recall = 0.18087, ndcg = 0.39271, precision = 0.34316, map = 0.10908\n",
                        "Epoch 11 (train)0.8s: train loss = 0.17564 = (mf)0.17374 + (embed)0.00190\n",
                        "Epoch 12 (train)0.8s: train loss = 0.16834 = (mf)0.16629 + (embed)0.00205\n",
                        "Epoch 13 (train)0.8s: train loss = 0.17051 = (mf)0.16833 + (embed)0.00218\n",
                        "Epoch 14 (train)0.8s: train loss = 0.16736 = (mf)0.16508 + (embed)0.00228\n",
                        "Epoch 15 (train)0.8s + (eval)0.2s: train loss = 0.16324 = (mf)0.16086 + (embed)0.00238, recall = 0.19198, ndcg = 0.40608, precision = 0.35048, map = 0.11731\n",
                        "Epoch 16 (train)0.8s: train loss = 0.16130 = (mf)0.15882 + (embed)0.00249\n",
                        "Epoch 17 (train)0.8s: train loss = 0.15924 = (mf)0.15667 + (embed)0.00257\n",
                        "Epoch 18 (train)0.8s: train loss = 0.15797 = (mf)0.15531 + (embed)0.00266\n",
                        "Epoch 19 (train)0.8s: train loss = 0.15601 = (mf)0.15325 + (embed)0.00275\n",
                        "Epoch 20 (train)0.8s + (eval)0.2s: train loss = 0.15112 = (mf)0.14826 + (embed)0.00286, recall = 0.19605, ndcg = 0.41763, precision = 0.36098, map = 0.12163\n",
                        "Training time: 16.2290s\n",
                        "Ranking prediction time: 0.0451s\n"
                    ]
                },
                {
                    "name": "stderr",
                    "output_type": "stream",
                    "text": [
                        "100%|██████████| 5.78k/5.78k [00:00<00:00, 15.4kKB/s]\n"
                    ]
                },
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "Size of Movielens 1m: (1000209, 4)\n",
                        "\n",
                        "Computing als algorithm on Movielens 1m\n"
                    ]
                },
                {
                    "name": "stderr",
                    "output_type": "stream",
                    "text": [
                        "22/10/19 09:03:02 WARN TaskSetManager: Stage 588 contains a task of very large size (2759 KiB). The maximum recommended task size is 1000 KiB.\n",
                        "22/10/19 09:03:02 WARN TaskSetManager: Stage 589 contains a task of very large size (2759 KiB). The maximum recommended task size is 1000 KiB.\n"
                    ]
                },
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "Training time: 7.0365s\n",
                        "Rating prediction time: 0.0355s\n"
                    ]
                },
                {
                    "name": "stderr",
                    "output_type": "stream",
                    "text": [
                        "22/10/19 09:03:19 WARN Column: Constructing trivially true equals predicate, 'userID#2403 = userID#2403'. Perhaps you need to use aliases.\n",
                        "22/10/19 09:03:19 WARN TaskSetManager: Stage 1045 contains a task of very large size (2759 KiB). The maximum recommended task size is 1000 KiB.\n"
                    ]
                },
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "Ranking prediction time: 0.0491s\n"
                    ]
                },
                {
                    "name": "stderr",
                    "output_type": "stream",
                    "text": [
                        "22/10/19 09:03:19 WARN TaskSetManager: Stage 1046 contains a task of very large size (2759 KiB). The maximum recommended task size is 1000 KiB.\n",
                        "22/10/19 09:03:20 WARN TaskSetManager: Stage 1092 contains a task of very large size (2759 KiB). The maximum recommended task size is 1000 KiB.\n",
                        "                                                                                \r"
                    ]
                },
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "\n",
                        "Computing svd algorithm on Movielens 1m\n",
                        "Training time: 41.6351s\n",
                        "Rating prediction time: 2.8386s\n",
                        "Ranking prediction time: 190.6115s\n",
                        "\n",
                        "Computing sar algorithm on Movielens 1m\n",
                        "Training time: 3.2292s\n",
                        "Ranking prediction time: 1.9796s\n",
                        "\n",
                        "Computing ncf algorithm on Movielens 1m\n",
                        "Training time: 816.1049s\n",
                        "Ranking prediction time: 48.9872s\n",
                        "\n",
                        "Computing fastai algorithm on Movielens 1m\n",
                        "Training time: 663.2788s\n",
                        "Rating prediction time: 0.3985s\n",
                        "Ranking prediction time: 47.2290s\n",
                        "\n",
                        "Computing bpr algorithm on Movielens 1m\n",
                        "Training time: 66.0371s\n",
                        "Ranking prediction time: 27.4882s\n",
                        "\n",
                        "Computing bivae algorithm on Movielens 1m\n",
                        "Training time: 152.0912s\n",
                        "Ranking prediction time: 27.5727s\n",
                        "\n",
                        "Computing lightgcn algorithm on Movielens 1m\n",
                        "Already create adjacency matrix.\n",
                        "Already normalize adjacency matrix.\n",
                        "Using xavier initialization.\n",
                        "Epoch 1 (train)23.2s: train loss = 0.34771 = (mf)0.34712 + (embed)0.00059\n",
                        "Epoch 2 (train)22.8s: train loss = 0.27739 = (mf)0.27605 + (embed)0.00134\n",
                        "Epoch 3 (train)22.8s: train loss = 0.22916 = (mf)0.22690 + (embed)0.00226\n",
                        "Epoch 4 (train)22.8s: train loss = 0.20559 = (mf)0.20265 + (embed)0.00295\n",
                        "Epoch 5 (train)22.8s + (eval)1.9s: train loss = 0.18818 = (mf)0.18458 + (embed)0.00360, recall = 0.12101, ndcg = 0.36960, precision = 0.33409, map = 0.07156\n",
                        "Epoch 6 (train)22.8s: train loss = 0.17528 = (mf)0.17106 + (embed)0.00422\n",
                        "Epoch 7 (train)22.8s: train loss = 0.16460 = (mf)0.15977 + (embed)0.00482\n",
                        "Epoch 8 (train)22.8s: train loss = 0.15525 = (mf)0.14984 + (embed)0.00541\n",
                        "Epoch 9 (train)22.8s: train loss = 0.14808 = (mf)0.14214 + (embed)0.00595\n",
                        "Epoch 10 (train)22.8s + (eval)1.8s: train loss = 0.14349 = (mf)0.13703 + (embed)0.00647, recall = 0.13872, ndcg = 0.40819, precision = 0.37030, map = 0.08408\n",
                        "Epoch 11 (train)22.8s: train loss = 0.13819 = (mf)0.13122 + (embed)0.00696\n",
                        "Epoch 12 (train)22.8s: train loss = 0.13232 = (mf)0.12490 + (embed)0.00742\n",
                        "Epoch 13 (train)22.8s: train loss = 0.12975 = (mf)0.12188 + (embed)0.00787\n",
                        "Epoch 14 (train)22.8s: train loss = 0.12524 = (mf)0.11694 + (embed)0.00830\n",
                        "Epoch 15 (train)22.8s + (eval)1.8s: train loss = 0.12257 = (mf)0.11385 + (embed)0.00872, recall = 0.14381, ndcg = 0.41610, precision = 0.37805, map = 0.08683\n",
                        "Epoch 16 (train)22.8s: train loss = 0.11986 = (mf)0.11074 + (embed)0.00912\n",
                        "Epoch 17 (train)22.7s: train loss = 0.11695 = (mf)0.10744 + (embed)0.00952\n",
                        "Epoch 18 (train)23.0s: train loss = 0.11496 = (mf)0.10506 + (embed)0.00990\n",
                        "Epoch 19 (train)22.8s: train loss = 0.11245 = (mf)0.10217 + (embed)0.01028\n",
                        "Epoch 20 (train)22.8s + (eval)1.8s: train loss = 0.11068 = (mf)0.10004 + (embed)0.01063, recall = 0.14773, ndcg = 0.42390, precision = 0.38572, map = 0.08977\n",
                        "Training time: 463.8591s\n",
                        "Ranking prediction time: 0.5867s\n",
                        "\n",
                        "Computation finished\n",
                        "CPU times: user 55min 50s, sys: 8min 57s, total: 1h 4min 47s\n",
                        "Wall time: 56min 59s\n"
                    ]
                }
            ],
            "source": [
                "%%time\n",
                "\n",
                "# For each data size and each algorithm, a recommender is evaluated. \n",
                "cols = [\"Data\", \"Algo\", \"K\", \"Train time (s)\", \"Predicting time (s)\", \"RMSE\", \"MAE\", \"R2\", \"Explained Variance\", \"Recommending time (s)\", \"MAP\", \"nDCG@k\", \"Precision@k\", \"Recall@k\"]\n",
                "df_results = pd.DataFrame(columns=cols)\n",
                "\n",
                "for data_size in data_sizes:\n",
                "    # Load the dataset\n",
                "    df = movielens.load_pandas_df(\n",
                "        size=data_size,\n",
                "        header=[DEFAULT_USER_COL, DEFAULT_ITEM_COL, DEFAULT_RATING_COL, DEFAULT_TIMESTAMP_COL]\n",
                "    )\n",
                "    print(\"Size of Movielens {}: {}\".format(data_size, df.shape))\n",
                "    \n",
                "    # Split the dataset\n",
                "    df_train, df_test = python_stratified_split(df,\n",
                "                                                ratio=0.75, \n",
                "                                                min_rating=1, \n",
                "                                                filter_by=\"item\", \n",
                "                                                col_user=DEFAULT_USER_COL, \n",
                "                                                col_item=DEFAULT_ITEM_COL\n",
                "                                                )\n",
                "   \n",
                "    # Loop through the algos\n",
                "    for algo in algorithms:\n",
                "        print(f\"\\nComputing {algo} algorithm on Movielens {data_size}\")\n",
                "          \n",
                "        # Data prep for training set\n",
                "        train = prepare_training_data.get(algo, lambda x,y:(x,y))(df_train, df_test)\n",
                "        \n",
                "        # Get model parameters\n",
                "        model_params = params[algo]\n",
                "          \n",
                "        # Train the model\n",
                "        model, time_train = trainer[algo](model_params, train)\n",
                "        print(f\"Training time: {time_train}s\")\n",
                "                \n",
                "        # Predict and evaluate\n",
                "        train, test = prepare_metrics_data.get(algo, lambda x,y:(x,y))(df_train, df_test)\n",
                "        \n",
                "        if \"rating\" in metrics[algo]:   \n",
                "            # Predict for rating\n",
                "            preds, time_rating = rating_predictor[algo](model, test)\n",
                "            print(f\"Rating prediction time: {time_rating}s\")\n",
                "            \n",
                "            # Evaluate for rating\n",
                "            ratings = rating_evaluator[algo](test, preds)\n",
                "        else:\n",
                "            ratings = None\n",
                "            time_rating = np.nan\n",
                "        \n",
                "        if \"ranking\" in metrics[algo]:\n",
                "            # Predict for ranking\n",
                "            top_k_scores, time_ranking = ranking_predictor[algo](model, test, train)\n",
                "            print(f\"Ranking prediction time: {time_ranking}s\")\n",
                "            \n",
                "            # Evaluate for rating\n",
                "            rankings = ranking_evaluator[algo](test, top_k_scores, DEFAULT_K)\n",
                "        else:\n",
                "            rankings = None\n",
                "            time_ranking = np.nan\n",
                "            \n",
                "        # Record results\n",
                "        summary = generate_summary(data_size, algo, DEFAULT_K, time_train, time_rating, ratings, time_ranking, rankings)\n",
                "        df_results.loc[df_results.shape[0] + 1] = summary\n",
                "        \n",
                "print(\"\\nComputation finished\")\n"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## Results"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 17,
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "text/html": [
                            "<div>\n",
                            "<style scoped>\n",
                            "    .dataframe tbody tr th:only-of-type {\n",
                            "        vertical-align: middle;\n",
                            "    }\n",
                            "\n",
                            "    .dataframe tbody tr th {\n",
                            "        vertical-align: top;\n",
                            "    }\n",
                            "\n",
                            "    .dataframe thead th {\n",
                            "        text-align: right;\n",
                            "    }\n",
                            "</style>\n",
                            "<table border=\"1\" class=\"dataframe\">\n",
                            "  <thead>\n",
                            "    <tr style=\"text-align: right;\">\n",
                            "      <th></th>\n",
                            "      <th>Data</th>\n",
                            "      <th>Algo</th>\n",
                            "      <th>K</th>\n",
                            "      <th>Train time (s)</th>\n",
                            "      <th>Predicting time (s)</th>\n",
                            "      <th>RMSE</th>\n",
                            "      <th>MAE</th>\n",
                            "      <th>R2</th>\n",
                            "      <th>Explained Variance</th>\n",
                            "      <th>Recommending time (s)</th>\n",
                            "      <th>MAP</th>\n",
                            "      <th>nDCG@k</th>\n",
                            "      <th>Precision@k</th>\n",
                            "      <th>Recall@k</th>\n",
                            "    </tr>\n",
                            "  </thead>\n",
                            "  <tbody>\n",
                            "    <tr>\n",
                            "      <th>1</th>\n",
                            "      <td>100k</td>\n",
                            "      <td>als</td>\n",
                            "      <td>10</td>\n",
                            "      <td>6.8526</td>\n",
                            "      <td>0.0587</td>\n",
                            "      <td>0.962863</td>\n",
                            "      <td>0.747088</td>\n",
                            "      <td>0.258405</td>\n",
                            "      <td>0.255016</td>\n",
                            "      <td>0.0782</td>\n",
                            "      <td>0.004697</td>\n",
                            "      <td>0.046619</td>\n",
                            "      <td>0.049629</td>\n",
                            "      <td>0.016688</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>2</th>\n",
                            "      <td>100k</td>\n",
                            "      <td>svd</td>\n",
                            "      <td>10</td>\n",
                            "      <td>4.0902</td>\n",
                            "      <td>0.2698</td>\n",
                            "      <td>0.938681</td>\n",
                            "      <td>0.742690</td>\n",
                            "      <td>0.291967</td>\n",
                            "      <td>0.291971</td>\n",
                            "      <td>13.8704</td>\n",
                            "      <td>0.012873</td>\n",
                            "      <td>0.095930</td>\n",
                            "      <td>0.091198</td>\n",
                            "      <td>0.032783</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>3</th>\n",
                            "      <td>100k</td>\n",
                            "      <td>sar</td>\n",
                            "      <td>10</td>\n",
                            "      <td>0.3344</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>0.0836</td>\n",
                            "      <td>0.113028</td>\n",
                            "      <td>0.388321</td>\n",
                            "      <td>0.333828</td>\n",
                            "      <td>0.183179</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>4</th>\n",
                            "      <td>100k</td>\n",
                            "      <td>ncf</td>\n",
                            "      <td>10</td>\n",
                            "      <td>66.8339</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>3.5393</td>\n",
                            "      <td>0.108609</td>\n",
                            "      <td>0.398754</td>\n",
                            "      <td>0.349735</td>\n",
                            "      <td>0.181576</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>5</th>\n",
                            "      <td>100k</td>\n",
                            "      <td>fastai</td>\n",
                            "      <td>10</td>\n",
                            "      <td>69.7469</td>\n",
                            "      <td>0.0338</td>\n",
                            "      <td>0.942754</td>\n",
                            "      <td>0.745138</td>\n",
                            "      <td>0.285810</td>\n",
                            "      <td>0.288468</td>\n",
                            "      <td>2.7415</td>\n",
                            "      <td>0.025896</td>\n",
                            "      <td>0.151481</td>\n",
                            "      <td>0.131813</td>\n",
                            "      <td>0.054491</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>6</th>\n",
                            "      <td>100k</td>\n",
                            "      <td>bpr</td>\n",
                            "      <td>10</td>\n",
                            "      <td>5.8205</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>1.9365</td>\n",
                            "      <td>0.132478</td>\n",
                            "      <td>0.441997</td>\n",
                            "      <td>0.388229</td>\n",
                            "      <td>0.212522</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>7</th>\n",
                            "      <td>100k</td>\n",
                            "      <td>bivae</td>\n",
                            "      <td>10</td>\n",
                            "      <td>11.4762</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>1.4382</td>\n",
                            "      <td>0.146126</td>\n",
                            "      <td>0.475077</td>\n",
                            "      <td>0.411771</td>\n",
                            "      <td>0.219145</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>8</th>\n",
                            "      <td>100k</td>\n",
                            "      <td>lightgcn</td>\n",
                            "      <td>10</td>\n",
                            "      <td>16.2290</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>0.0451</td>\n",
                            "      <td>0.121633</td>\n",
                            "      <td>0.417629</td>\n",
                            "      <td>0.360976</td>\n",
                            "      <td>0.196052</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>9</th>\n",
                            "      <td>1m</td>\n",
                            "      <td>als</td>\n",
                            "      <td>10</td>\n",
                            "      <td>7.0365</td>\n",
                            "      <td>0.0355</td>\n",
                            "      <td>0.858791</td>\n",
                            "      <td>0.677568</td>\n",
                            "      <td>0.413262</td>\n",
                            "      <td>0.408737</td>\n",
                            "      <td>0.0491</td>\n",
                            "      <td>0.002683</td>\n",
                            "      <td>0.030447</td>\n",
                            "      <td>0.036707</td>\n",
                            "      <td>0.011461</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>10</th>\n",
                            "      <td>1m</td>\n",
                            "      <td>svd</td>\n",
                            "      <td>10</td>\n",
                            "      <td>41.6351</td>\n",
                            "      <td>2.8386</td>\n",
                            "      <td>0.883017</td>\n",
                            "      <td>0.695366</td>\n",
                            "      <td>0.374910</td>\n",
                            "      <td>0.374911</td>\n",
                            "      <td>190.6115</td>\n",
                            "      <td>0.008828</td>\n",
                            "      <td>0.089320</td>\n",
                            "      <td>0.082856</td>\n",
                            "      <td>0.021582</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>11</th>\n",
                            "      <td>1m</td>\n",
                            "      <td>sar</td>\n",
                            "      <td>10</td>\n",
                            "      <td>3.2292</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>1.9796</td>\n",
                            "      <td>0.066214</td>\n",
                            "      <td>0.313502</td>\n",
                            "      <td>0.279692</td>\n",
                            "      <td>0.111135</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>12</th>\n",
                            "      <td>1m</td>\n",
                            "      <td>ncf</td>\n",
                            "      <td>10</td>\n",
                            "      <td>816.1049</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>48.9872</td>\n",
                            "      <td>0.065931</td>\n",
                            "      <td>0.357964</td>\n",
                            "      <td>0.327249</td>\n",
                            "      <td>0.111665</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>13</th>\n",
                            "      <td>1m</td>\n",
                            "      <td>fastai</td>\n",
                            "      <td>10</td>\n",
                            "      <td>663.2788</td>\n",
                            "      <td>0.3985</td>\n",
                            "      <td>0.874907</td>\n",
                            "      <td>0.695758</td>\n",
                            "      <td>0.386338</td>\n",
                            "      <td>0.388787</td>\n",
                            "      <td>47.2290</td>\n",
                            "      <td>0.026237</td>\n",
                            "      <td>0.184787</td>\n",
                            "      <td>0.168494</td>\n",
                            "      <td>0.056014</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>14</th>\n",
                            "      <td>1m</td>\n",
                            "      <td>bpr</td>\n",
                            "      <td>10</td>\n",
                            "      <td>66.0371</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>27.4882</td>\n",
                            "      <td>0.083546</td>\n",
                            "      <td>0.390911</td>\n",
                            "      <td>0.357777</td>\n",
                            "      <td>0.142510</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>15</th>\n",
                            "      <td>1m</td>\n",
                            "      <td>bivae</td>\n",
                            "      <td>10</td>\n",
                            "      <td>152.0912</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>27.5727</td>\n",
                            "      <td>0.094632</td>\n",
                            "      <td>0.438106</td>\n",
                            "      <td>0.399039</td>\n",
                            "      <td>0.149889</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>16</th>\n",
                            "      <td>1m</td>\n",
                            "      <td>lightgcn</td>\n",
                            "      <td>10</td>\n",
                            "      <td>463.8591</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>NaN</td>\n",
                            "      <td>0.5867</td>\n",
                            "      <td>0.089775</td>\n",
                            "      <td>0.423900</td>\n",
                            "      <td>0.385721</td>\n",
                            "      <td>0.147728</td>\n",
                            "    </tr>\n",
                            "  </tbody>\n",
                            "</table>\n",
                            "</div>"
                        ],
                        "text/plain": [
                            "    Data      Algo   K Train time (s) Predicting time (s)      RMSE       MAE  \\\n",
                            "1   100k       als  10         6.8526              0.0587  0.962863  0.747088   \n",
                            "2   100k       svd  10         4.0902              0.2698  0.938681  0.742690   \n",
                            "3   100k       sar  10         0.3344                 NaN       NaN       NaN   \n",
                            "4   100k       ncf  10        66.8339                 NaN       NaN       NaN   \n",
                            "5   100k    fastai  10        69.7469              0.0338  0.942754  0.745138   \n",
                            "6   100k       bpr  10         5.8205                 NaN       NaN       NaN   \n",
                            "7   100k     bivae  10        11.4762                 NaN       NaN       NaN   \n",
                            "8   100k  lightgcn  10        16.2290                 NaN       NaN       NaN   \n",
                            "9     1m       als  10         7.0365              0.0355  0.858791  0.677568   \n",
                            "10    1m       svd  10        41.6351              2.8386  0.883017  0.695366   \n",
                            "11    1m       sar  10         3.2292                 NaN       NaN       NaN   \n",
                            "12    1m       ncf  10       816.1049                 NaN       NaN       NaN   \n",
                            "13    1m    fastai  10       663.2788              0.3985  0.874907  0.695758   \n",
                            "14    1m       bpr  10        66.0371                 NaN       NaN       NaN   \n",
                            "15    1m     bivae  10       152.0912                 NaN       NaN       NaN   \n",
                            "16    1m  lightgcn  10       463.8591                 NaN       NaN       NaN   \n",
                            "\n",
                            "          R2  Explained Variance Recommending time (s)       MAP    nDCG@k  \\\n",
                            "1   0.258405            0.255016                0.0782  0.004697  0.046619   \n",
                            "2   0.291967            0.291971               13.8704  0.012873  0.095930   \n",
                            "3        NaN                 NaN                0.0836  0.113028  0.388321   \n",
                            "4        NaN                 NaN                3.5393  0.108609  0.398754   \n",
                            "5   0.285810            0.288468                2.7415  0.025896  0.151481   \n",
                            "6        NaN                 NaN                1.9365  0.132478  0.441997   \n",
                            "7        NaN                 NaN                1.4382  0.146126  0.475077   \n",
                            "8        NaN                 NaN                0.0451  0.121633  0.417629   \n",
                            "9   0.413262            0.408737                0.0491  0.002683  0.030447   \n",
                            "10  0.374910            0.374911              190.6115  0.008828  0.089320   \n",
                            "11       NaN                 NaN                1.9796  0.066214  0.313502   \n",
                            "12       NaN                 NaN               48.9872  0.065931  0.357964   \n",
                            "13  0.386338            0.388787               47.2290  0.026237  0.184787   \n",
                            "14       NaN                 NaN               27.4882  0.083546  0.390911   \n",
                            "15       NaN                 NaN               27.5727  0.094632  0.438106   \n",
                            "16       NaN                 NaN                0.5867  0.089775  0.423900   \n",
                            "\n",
                            "    Precision@k  Recall@k  \n",
                            "1      0.049629  0.016688  \n",
                            "2      0.091198  0.032783  \n",
                            "3      0.333828  0.183179  \n",
                            "4      0.349735  0.181576  \n",
                            "5      0.131813  0.054491  \n",
                            "6      0.388229  0.212522  \n",
                            "7      0.411771  0.219145  \n",
                            "8      0.360976  0.196052  \n",
                            "9      0.036707  0.011461  \n",
                            "10     0.082856  0.021582  \n",
                            "11     0.279692  0.111135  \n",
                            "12     0.327249  0.111665  \n",
                            "13     0.168494  0.056014  \n",
                            "14     0.357777  0.142510  \n",
                            "15     0.399039  0.149889  \n",
                            "16     0.385721  0.147728  "
                        ]
                    },
                    "execution_count": 17,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "df_results"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "# Record results for tests - ignore this cell\n",
                "for algo in algorithms:\n",
                "    store_metadata(algo, df_results.loc[df_results[\"Algo\"] == algo, \"nDCG@k\"].values[0])\n"
            ]
        }
    ],
    "metadata": {
        "kernelspec": {
            "display_name": "Python 3.7.13 ('reco')",
            "language": "python",
            "name": "python3"
        },
        "language_info": {
            "codemirror_mode": {
                "name": "ipython",
                "version": 3
            },
            "file_extension": ".py",
            "mimetype": "text/x-python",
            "name": "python",
            "nbconvert_exporter": "python",
            "pygments_lexer": "ipython3",
            "version": "3.7.13"
        },
        "vscode": {
            "interpreter": {
                "hash": "2d9774f375d93db4a064c1fe757ad48aac12718e2d70725b436da9188beb8cf3"
            }
        }
    },
    "nbformat": 4,
    "nbformat_minor": 4
}
